<template>
  <div class="login">
    <div class="left">
      <!-- 顶部标题部分 -->
      <div class="title">
        <img src="@/assets/img/logo_icon.png" alt="" class="t1" />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <!-- form  表单 -->
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            v-model="form.phone"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            show-password
            v-model="form.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="请输入验证码"
                v-model="form.code"
                prefix-icon="el-icon-key"
              >
              </el-input>
            </el-col>
            <el-col :span="8">
              <img class="codeImg" :src="baseURL+codeUrl " alt=""  @click='refleshCode'/>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true"
            >我已阅读并同意<el-link type="primary">用户协议</el-link>和<el-link
              type="primary"
              >隐私条框</el-link
            ></el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="toLogin">登录</el-button>
          <br />
          <el-button type="primary" class="btn" @click="toSubmit">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右侧登录背景图 -->
    <img src="@/assets/img/login_img.png" alt="" />
    <register ref="register"></register>
  </div>
</template>
<script>
import register from '../register'
import {login} from '@/api/login.js'
import {saveLocal,getLocal} from '@/utils/local.js'
export default {
  components:{
register,
  },
  created(){
    if(getLocal('token')){
      this.$router.push('./layout')
    }
  },
  data() {
    
    return {
      codeUrl:'/captcha?type=login',
      baseURL:process.env.VUE_APP_URL,
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules:{
        phone:[
          {required:true,message:'请输入手机号',trigger:"change"},
          {validator:(rule,value,callback)=>{
            const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
            if(_reg.test(value)){
              callback()
            }else{
              callback(new Error('请正确输入手机号码'))
            }
          }}
        ],
        password:[
          {required:true,message:'请输入密码',trigger:"change"},
          {min:6,max:12,message:'请正确输入密码',trigger:'change'}
        ],
        code:[
          {required:true,message:'请输入验证码',trigger:"change"},
          {min:4,max:4,message:'请正确输入验证码',trigger:'change'}
        ],
        isPass:[
          {required:true,message:'请勾选协议',trigger:"change"},
        ],
      }
    };
  },
  methods:{
    toLogin(){
      this.$refs.form.validate(result=>{
        if(result){
          login(this.form).then(res=>{
            saveLocal('token',res.data.data.token)
            this.$router.push('/layout')
          })
        }else{
          this.$message.error('登录失败')
        }
      })
    },
    toSubmit(){
      this.$refs.register.isShow=true
    },
    refleshCode(){
      this.codeUrl='/captcha?type=login&xxx='+Math.random()*9999
    }
  }
};
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;

  .left {
    padding: 42px;
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    .form {
      margin-top: 30px;
      .btn {
        display: block;
        width: 100%;
      }
      .codeImg {
        width: 100%;
        height: 40px;
      }
    }

    .title {
      display: flex;
      align-items: center;
      .t1 {
        width: 22px;
        height: 17px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 18px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
      .t4 {
        margin-left: 18px;
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
  }
}
</style>
